﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Itfort
{
	/// <summary>
	/// 
	/// </summary>
	public static class MyReflection
	{
		/// <summary>
		/// 类型的属性信息的全局缓存。
		/// </summary>
		private static Dictionary<Type, PropertyInfoDictionary> TypePropertyInfoDictionaryCache = new Dictionary<Type, PropertyInfoDictionary>();

		/// <summary>
		/// 获取类型的属性信息。
		/// </summary>
		/// <param name="type">类型实例。</param>
		/// <returns>返回类型实例的公开实例属性。</returns>
		public static Dictionary<String, PropertyInfo> GetTypePropertyDictionary(Type type)
		{
			PropertyInfoDictionary typePropertyInfoDictionary;
			if (!TypePropertyInfoDictionaryCache.TryGetValue(type, out typePropertyInfoDictionary))
			{// 全局缓存中不存在类型的属性集
				typePropertyInfoDictionary = new PropertyInfoDictionary();
				var list = type.GetProperties();
				foreach (var item in list)
				{
					typePropertyInfoDictionary.Add(item.Name, item);
				}
				TypePropertyInfoDictionaryCache[type] = typePropertyInfoDictionary;
			}
			return typePropertyInfoDictionary;
		}

		/// <summary>
		/// 类型属性字典
		/// </summary>
		public class PropertyInfoDictionary : Dictionary<String, PropertyInfo>
		{

		}
	}
}
